﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class FactoryUI : MonoBehaviour {

	public GameObject UnitPrefab;
	public GameObject SlotPrefab;

	//NGUI
	//public UIGrid UnitContainer;
	//public UIGrid SlotContainer;
	
	//UGUI
	public Transform UnitsPanel;
	public Transform SlotsPanel;
	public Vector2 gridOffset;
	
	private GameFactory factory;
	private Rect unitsRect;
	
	private List<Transform> unitSlots = new List<Transform>();
	
	// Get initial dimensions
	void Awake () {
		//unitRect = SlotPrefab.GetComponent<RectTransform>().rect;
		if (SlotsPanel!=null) {
			unitsRect = SlotsPanel.GetComponent<RectTransform>().rect;
			//Debug.Log("FactoryUI : unitsRect=" + unitsRect);
		}
	}
	
	// Use this for initialization
	void Start () {
		//factory = GameFactory.instance;
		
		UnitPrefab.gameObject.SetActive(false);
		SlotPrefab.gameObject.SetActive(false);
	
		Vector3 offset = new Vector3();
		
		//fill the panel with 
		XmlDataBase data = GameData.instance.UnitsData;
		foreach (KeyValuePair<string, XmlEntry> item in data.entryMap) {
			//Debug.Log( "FactoryUI : key=" + item.Key );
			
			GameObject unitObj = (GameObject)Instantiate( UnitPrefab );
			UnitUI unitUI = unitObj.GetComponent<UnitUI>();
			unitUI.SetUnit(item.Value);
			unitObj.SetActive(true);
			
			//if (UnitContainer!=null)
			//	UnitContainer.AddChild(unitObj.transform);
				
			if (UnitsPanel!=null) {
				unitObj.transform.SetParent( UnitsPanel.transform, false );
				//Debug.Log("unitObj.transform.position=" + unitObj.transform.position);
				unitObj.transform.position += offset;
				
				offset.x += gridOffset.x;
				if (offset.x > unitsRect.width - gridOffset.x) {
					offset.x = 0.0f;
					offset.y -= gridOffset.y;
				}
			}
		}
		
		/*if (UnitsPanel!=null) { //TODO- grow the panel to hold all units
			RectTransform rectTransform = UnitsPanel.GetComponent<RectTransform>();
			//Debug.Log("rectTransform.offsetMin=" + rectTransform.offsetMin);
			//Debug.Log("rectTransform.offsetMax=" + rectTransform.offsetMax);
		}*/
	}
	
	// Update is called once per frame
	void Update () {
		if (GameFactory.instance != null) {
			if (factory == null && GameFactory.instance.isLoaded) {
				factory = GameFactory.instance;
				Load();
			}
		}
	}
	
	void Load() {
		//mark units unlocked
		foreach (Transform unitSlot in unitSlots)
			Destroy(unitSlot.gameObject);
		
		unitSlots.Clear();
		
		//TODO clear all production slots
		//the production queue
		foreach (string unit in factory.units) {
			AddUnit( unit );
		}
	}

	public void AddUnit( string unit ) {
		XmlEntry data = GameData.instance.UnitsData.find(unit);
	
		GameObject slotObj = (GameObject)Instantiate( SlotPrefab );
		slotObj.transform.SetParent(SlotPrefab.transform.parent, false);
		slotObj.transform.localPosition += new Vector3(gridOffset.x, 0.0f) * unitSlots.Count;
		slotObj.SetActive(true);
		
		GameObject unitObj = (GameObject)Instantiate( UnitPrefab );
		unitObj.GetComponent<UnitUI>().SetUnit( data );
		unitObj.GetComponent<UnitUI>().enabled = false;
		unitObj.GetComponent<Button>().enabled = false;
		unitObj.gameObject.SetActive(true);
		unitObj.transform.SetParent( slotObj.transform.FindChild("Unit"), false );
		unitObj.transform.localPosition = new Vector3();
			
		unitSlots.Add(slotObj.transform);
	}

	// Add unit to queue
	public void OnAddUnitClick( UnitUI unitUI ) {
		Debug.Log("FactoryUI.OnAddUnitClick : object.name=" + unitUI.gameObject.name);
		if (factory==null) return;
		
		string unitId = unitUI.unitData.get("key");
		factory.AddUnit(unitId); //add to model
		AddUnit(unitId); //add to UI
	}
			
	// Remove unit from queue
	public void OnRemoveUnitClick( ) {
		//TODO get the unit that is being crafted
		if (factory==null) return;
		
		//remove from factory
		//remove from UI
	}
}
